package GateWars;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import List.PlayerNode;
import List.SortedList;

public class GateWarsFactory {
    private static String ssID = "";
    private static GateWarsManager gwm = null;
    private static PageContentParser pcp = new PageContentParser();
    private static SortedList players = new SortedList();
    private static File playerLog;
    private static BufferedReader in;
    
    public GateWarsFactory(){}
    
    static{
        try {
            playerLog = new File(".\\playerLog.txt");
            if(!playerLog.exists()){
                playerLog.createNewFile();
            }
            in = new BufferedReader(new FileReader(playerLog));            
            //Write("07monkeys02oi");
            //Write("01a");
            //ReadPlayer();
        } catch (Exception e) {}
    }
    
    private static void Write(String s){
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(playerLog, true));//append
            out.write(s);
            out.flush();
            out.close();
        } catch (IOException e) {}
    }
    
    private static char[] Read(int length){
        try {
            char[] cbuf = new char[10000];        
            int n = in.read(cbuf, 0, length);
            if(n == -1){
                return null;
            }
            return cbuf;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static PlayerNode ReadPlayer(){
        char[] c;
        c = Read(2);
        if(c != null)
        {
            String s = new String(c).trim();
            int n = new Integer(s);
            while(n > 0){
                c = Read(n);
                if(c != null)
                {
                    String att = new String(c).trim();
                    System.out.println(att);
                    c = Read(2);
                    if(c != null)
                    {
                        n = new Integer(new String(c).trim());
                    }
                    else
                    {
                        n = -1;
                    }
                }
                else
                {
                    n = -1;
                }
            }
        }
        return null;
    }
    
    public static void parse(String sb)
    {
        pcp.parse(sb);
    }
    
    public static synchronized GateWarsManager getGateWarsManager(){
        if(gwm == null)
        {
            gwm = new GateWarsManager("");
        }
        return gwm;
    }    
    
    public static synchronized void newSSID(String sessionID){
        if(gwm == null || !ssID.equals(sessionID))
        {
            if(gwm != null){
                gwm.logOut();
            }
            gwm = new GateWarsManager(sessionID);
            Thread t = new Thread(gwm);
            t.start();
            ssID = sessionID;
            System.out.println("new Session");
        }
    }
    
    public static synchronized void addPlayer(PlayerNode player){
        players.addPlayer(player);
    }
    
    public static PlayerNode getLargestNaq(){
        return players.getLargestNaq();
    }
    
    public static PlayerNode getLargestArmy(){
        return players.getLargestArmy();
    }

    /**
     * global save and load will perform the same actions as save/load and also 
     * save players.
     */
    private static void globalSave(){
        try{
            in.close();
        }catch (Exception e){}
    }
    private static void globalLoad(){}
}
